<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/30
 * Time: 14:43
 * 提现记录
 */
require_once 'common.php';
$row = WoodyApp::model('dlusermoney')->get_usermoney_by_userid($_SESSION['user_id']);
if ($row) {
    $total_money = $row['unpaid'];
    $total_freeze = $row['freeze'];
}
$noShipOrderMoney = WoodyApp::model('dlusermoney')->userIncome($_SESSION['user_id'], 3);
$total_money += $noShipOrderMoney;
$result = WoodyApp::model('dlorders')->get_orders_list('', '', "is_ship=0 and userid='" . $_SESSION['user_id'] . "' AND date(addtime)='" . date('Y-m-d') . "' AND (is_status=1 OR is_status=2)");
$today_money = 0;
if ($result) {
    foreach ($result as $key => $row) {
        $today_money += $row['income_money'];
    }
}
$apply_money = number_format($total_money, 2, '.', '');
$total_money = $total_money + $today_money;
$config = WoodyApp::model('config')->get_config();
$action = $_GET['action'];
if($action == ''){//提现记录
    require View::getView('takecash');
    View::Output();
}elseif($action == 'ajaxpages'){
    $pages = $_POST['pages'] ? $_POST['pages'] : 1;
    $pagesize = 5;
    $cons = "userid=" . $_SESSION['user_id'] . "";
    $offset = ($pages -1) * 5;
    $lists = WoodyApp::model('payments')->get_payments_list($pagesize, $offset, $cons);
    if($lists){
        foreach ($lists as $key=>$val){
            if($val['is_state'] == 0){
                $lists[$key]['is_state_str'] = '待处理';
                $lists[$key]['price'] = 0.00;
            }elseif ($val['is_state'] == 1){
                $lists[$key]['is_state_str'] = '已完成';
                $lists[$key]['price'] = number_format($val['money'] - $val['fee'], 2, '.', '');
            }elseif ($val['is_state'] == 2){
                $lists[$key]['is_state_str'] = '已拒绝';
                $lists[$key]['price'] = 0.00;
            }
            if($val['pid'] == 1){
                $lists[$key]['type'] = '自动结算';
            }else{
                $lists[$key]['type'] = '商户提款';
            }
        }
        $Err['code'] = 1;
        $Err['msg'] = $lists;
    }else{
        $Err['code'] = -1;
        $Err['msg'] = '暂无更多数据！';
    }
    echo json_encode($Err);
    exit;
}elseif ($action == 'apply'){//提现页面
    if (!$_SESSION['user']['is_apply_settlement']) {
        $msg = '<span class="red">当前账户未开通申请结算功能！</span>';
        $img = 'error';
        require View::getView('prompt');
        View::Output();
        exit;
    }
    $userinfo_arr = WoodyApp::model('users')->get_userinfo_by_userid($_SESSION['user_id']);
    if ($userinfo_arr['realname'] == '') {
        $msg = '<span class="red">当前账户未设置收款信息，暂不能申请结算！</span>';
        $img = 'error';
        require View::getView('prompt');
        View::Output();
        exit;
    }

    if ($config['takecash_state'] == 1) {//关闭提现功能，全部银行代付结算无需申请
        $msg = '<span class="red">' . $config['takecash_msgtip'] .'</span>';
        $img = 'error';
        require View::getView('prompt');
        View::Output();
        exit;
    }

    if ($apply_money < $config['takecash']) {//设置最低提现金额
        $msg = '<span class="red">当前账户可申请结算金额不足，暂不能申请结算！</span>';
        $img = 'error';
        require View::getView('prompt');
        View::Output();
        exit;
    }

    $tnt = WoodyApp::model('payments')->count('payments', "pid<>1 AND userid=" . $_SESSION['user_id'] . " AND date(addtime)='" . date('Y-m-d') . "'");
    if ($tnt >= $config['takecash_times']) {//多次提交
        $msg = '<span class="red">' . $config['takecash_times_msg'] . '</span>';
        $img = 'error';
        require View::getView('prompt');
        View::Output();
        exit;
    }

    $data = WoodyApp::model('users')->get_userinfo_by_userid($_SESSION['user_id']);
    if ($data) {
        $ptype = $data['ptype'];
        switch ($ptype) {
            case '1':
                $payname = '网上银行';
                break;
            case '2':
                $payname = '支付宝';
                break;
            case '3':
                $payname = '财付通';
                break;
            default:
                $payname = '';
        }
        $realname = $data['realname'];
        $bank = $data['bank'];
        $card = $data['card'];
        $addr = $data['addr'];
        $alipay = $data['alipay'];
        $tenpay = $data['tenpay'];
    } else {
        $ptype = 0;
        $realname = '';
        $bank = '';
        $card = '';
        $addr = '';
        $alipay = '';
        $tenpay = '';
    }
    require View::getView('apply');
    View::Output();
    exit;
}elseif ($action == 'add'){//提现处理
    if (!$_SESSION['login_user_safe_key']) {
        $msg = '<span class="red">安全密码不存在！</span>';
        $img = 'error';
        require View::getView('prompt');
        View::Output();
        exit;
    }
    $chkcode = $_POST['chkcode'];
    $money = $_POST['money'];
    $tf = $config['takecash_f'];
    $tt = $config['takecash_t'];
    $tn = date('G');
    if ($tf == $tt) {
        $msg = '<span class="red">当前系统已暂停商户提现功能(周六周日不结算)，暂不能申请提现!</span>';
        $img = 'error';
        require View::getView('prompt');
        View::Output();
        exit;
    }

    $tnt = WoodyApp::model('payments')->count('payments', "pid<>1 AND userid=" . $_SESSION['user_id'] . " AND date(addtime)='" . date('Y-m-d') . "'");
    if ($tnt >= $config['takecash_times']) {//多次提交
        $msg = '<span class="red">' . $config['takecash_times_msg'] . '</span>';
        $img = 'error';
        require View::getView('prompt');
        View::Output();
        exit;
    }
    if ($tn < $tf || $tn > $tt) {
        $msg = '<span class="red">当前系统允许提现时间从 ' . $tf . '点至 ' . $tt . '点，其他时间暂不能申请提现!</span>';
        $img = 'error';
        require View::getView('prompt');
        View::Output();
        exit;
    }
    if ($money > $apply_money) {
        $msg = '<span class="red">申请结算失败！</span>';
        $img = 'error';
        require View::getView('prompt');
        View::Output();
        exit;
    }
    if ($config['takecash'] > 0 && $money < $config['takecash']) {
        $msg = '<span class="red">申请结算失败，当前最低提现金额为' . $config['takecash'] . '！</span>';
        $img = 'error';
        require View::getView('prompt');
        View::Output();
        exit;
    }
    $userBank = WoodyApp::model('users')->get_userinfo_by_userid($_SESSION['user_id']);
    if ($userBank['ptype'] == 1) {
        $ptype = $userBank['bank'];
        $account = $userBank['card'];
        $realname = $userBank['realname'];
    } elseif ($userBank['ptype'] == 2) {
        $ptype = '支付宝';
        $account = $userBank['alipay'];
        $realname = $userBank['realname'];
    } elseif ($userBank['ptype'] == 3) {
        $ptype = '财付通';
        $account = $userBank['tenpay'];
        $realname = $userBank['realname'];
    }
    $remark = '姓名:' . $realname . ' 方式：' . $ptype . ' 账号：' . $account;
    $data = array('userid' => $_SESSION['user_id'], 'remark' => $remark, 'money' => $money, 'addtime' => date('Y-m-d H:i:s'), 'pid' => 2);
    WoodyApp::model('payments')->insert('payments', $data);
    WoodyApp::model('usermoney')->updateMoney($_SESSION['user_id'], $money);

    //------推广部分写入spreadpayments表中结束------//
    $title = '商户[' . $_SESSION['user']['username'] . ']申请提现通知';
    $content = '商户 ' . $_SESSION['user']['username'] . ' 于 ' . date('Y-m-d H:i:s') . ' 申请提现，申请提现金额为 [' . $money . '] 元。';
    $data = array('from_user' => $_SESSION['user_id'], 'to_user' => 1, 'title' => $title, 'content' => $content, 'addtime' => date('Y-m-d H:i:s'));
    WoodyApp::model('message')->insert('message', $data);
    $msg = '<span class="green">申请结算成功，请等待客服处理！</span>';
    $img = 'error';
    require View::getView('prompt');
    View::Output();
}